package com.zhaojonhy.zhaopangwb.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.bean.Comment;
import com.zhaojonhy.zhaopangwb.bean.User;
import com.zhaojonhy.zhaopangwb.utils.ToastUtils;
import com.zhaojonhy.zhaopangwb.ui.UserInfoActivity;
import com.zhaojonhy.zhaopangwb.utils.DateUtils;
import com.zhaojonhy.zhaopangwb.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;

/**
 * Created by Administrator on 2016/9/22.
 */

public class StatusCommentAdapter extends BaseAdapter {

    private Context context ;
    private List<Comment> commments ;
    private ImageLoader imageLoader;
    public StatusCommentAdapter(Context context , List<Comment> commments ){
        this.context = context ;
        this.commments = commments ;
        imageLoader = ImageLoader.getInstance() ;
    }

    @Override
    public int getCount() {
        return commments.size() ;
    }

    @Override
    public Comment getItem(int position) {
        return commments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder ;
        if( convertView == null ){
            holder = new Holder() ;
            convertView = View.inflate(context,R.layout.item_comment,null) ;
            holder.ll_comments = (LinearLayout)convertView.findViewById(R.id.ll_comments) ;
            holder.tv_comment = (TextView)convertView.findViewById(R.id.tv_comment) ;
            holder.iv_avatar = (ImageView)convertView.findViewById(R.id.iv_avatar) ;
            holder.tv_subhead = (TextView)convertView.findViewById(R.id.tv_subhead) ;
            holder.tv_caption = (TextView)convertView.findViewById(R.id.tv_caption) ;
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag() ;
        }
        Comment comment = getItem(position);
        final User user = comment.getUser();

        imageLoader.displayImage(user.getProfile_image_url(), holder.iv_avatar);
        holder.tv_subhead.setText(user.getName());
        holder.tv_caption.setText(DateUtils.getShortTime(comment.getCreated_at()));
        SpannableString weiboContent = StringUtils.getWeiboContent(
                context, holder.tv_comment, comment.getText());
        holder.tv_comment.setText(weiboContent);
        holder.iv_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,UserInfoActivity.class) ;
                intent.putExtra("userName",user.getName()) ;
                context.startActivity(intent) ;
            }
        });

        holder.tv_subhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,UserInfoActivity.class) ;
                intent.putExtra("userName",user.getName()) ;
                context.startActivity(intent) ;
            }
        });
        holder.ll_comments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showToast(context, "回复评论", Toast.LENGTH_SHORT);
            }
        });
        return convertView;
    }

    private class Holder {
        private LinearLayout ll_comments ;
        private TextView tv_comment ;
        private ImageView iv_avatar ;
        private TextView tv_subhead ;
        private TextView tv_caption ;
    }
}
